﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using ArcEngine.Graphics;
using System.Xml;
using ArcEngine.Resources;
using System.ComponentModel;


namespace ArcEngine.GUI
{


	/// <summary>
	/// A GUI button
	/// </summary>
	public class Button : GuiBase
	{

		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="name"></param>
		public Button(string name) : base(name)
		{


			// Default button size
			Size = new Size(32, 32);
		}




		#region IO routines

		///
		///<summary>
		/// Save the button to a xml node
		/// </summary>
		///
		public override bool Save(XmlWriter xml)
		{
			if (xml == null)
				return false;

			xml.WriteStartElement("button");
			xml.WriteAttributeString("name", Name);

			xml.WriteStartElement("tileset");
			xml.WriteAttributeString("name", TileSetName);
			xml.WriteAttributeString("id", TileID.ToString());
			xml.WriteEndElement();

			xml.WriteStartElement("texturelayout");
			xml.WriteAttributeString("name", TextureLayout.ToString());
			xml.WriteEndElement();



			base.Save(xml);


			xml.WriteEndElement();

			return true;
		}


		/// <summary>
		/// Loads the button from a xml node
		/// </summary>
		/// <param name="xml"></param>
		/// <returns></returns>
		public override bool Load(XmlNode xml)
		{
			foreach (XmlNode node in xml)
			{
				switch (node.Name.ToLower())
				{
					// Unknown element, give it to the base
					default:
					{
						base.Load(node);
					}
					break;


					case "tileset":
					{
						TileSetName = node.Attributes["name"].Value;
						TileID = int.Parse(node.Attributes["id"].Value.ToString());
					}
					break;
				}
			}

			return true;
		}

		#endregion



		#region Methods

		/// <summary>
		/// Draws the button
		/// </summary>
		public override void Draw()
		{

			// Background color
			if (BgColor.A > 0)
			{
				Video.Color = BgColor;
				Video.Rectangle(Rectangle, true);
			}


			// Tile set ?
			if (TileSet != null)
			{
				Video.Color = Color;

				TileSet.Bind();
				TileSet.Draw(TileID, Rectangle, TextureLayout);
			}


			Video.Color = Color.White;
		}


		/// <summary>
		/// Resize the button to fit the background texture size
		/// </summary>
		public void ResizeToFitTexture()
		{
			if (Tile == null)
				return;

			Size = Tile.Size;
		}

		#endregion



		#region Properties



		/// <summary>
		/// Gets/Sets the TileSet name to use
		/// </summary>
		[TypeConverter(typeof(TileSetEnumerator))]
		public string TileSetName
		{
			get
			{
				return tileSetName;
			}
			set
			{
				tileSetName = value;

				tileSet = ResourceManager.GetTileSet(value);
			}
		}
		string tileSetName;
 
         

		/// <summary>
		/// Gets the TileSet to use
		/// </summary>
		[Browsable(false)]
		public TileSet TileSet
		{
			get
			{
				return tileSet;
			}
		}
		TileSet tileSet;



		/// <summary>
		/// TileID in the TileSet to use
		/// </summary>
		public int TileID
		{
			get
			{
				return tileID;
			}
			set
			{
				tileID = value;
			}
		}
		int tileID;


		/// <summary>
		/// Gets the used tile
		/// </summary>
		public Tile Tile
		{
			get
			{
				if (TileSet == null)
					return null;

				return TileSet.GetTile(TileID);

			}
		}


		/// <summary>
		/// Texture behaviour
		/// </summary>
		[TypeConverter(typeof(TextureLayout))]
		public TextureLayout TextureLayout
		{
			get
			{
				return textureLayout;
			}
			set
			{
				textureLayout = value;
			}
		}
		TextureLayout textureLayout;
 
         
        

		#endregion

	}
}
